package it.lolled.core;

import it.lolled.entity.Post;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;



public class HttpSend {
	private final String USER_AGENT = "Mozilla/5.0";

	public static void main(String[] args) {
		Post post = new Post();

		post.setId("ad6PByQ");
		post.setImg("http://img-9gag-lol.9cache.com/photo/ad6PByQ_460s.jpg");
		post.setTitle("Currently assuming fetal position");
		post.setSource("9gag");
		new HttpSend().postInsert(post);
	}

	// public void postInsert(Post post) {
	// String url =
	// "http://loleconomy.altervista.org/src/service/PostService.php?action=insert";
	// InputStream in = null;
	//
	// try {
	// HttpClient client = new HttpClient();
	//
	//
	//
	// PostMethod method = new PostMethod(url);
	// //Add any parameter if u want to send it with Post req.
	// method.addParameter("secret", "l0renz0");
	// method.addParameter("id", post.getId());
	// method.addParameter("title", post.getTitle());
	// method.addParameter("image", post.getImg());
	// method.addParameter("source", post.getSource());
	//
	// int statusCode = client.executeMethod(method);
	//
	// if (statusCode != -1) {
	// in = method.getResponseBodyAsStream();
	// }
	//
	//
	// int size = in.available();
	// byte[] buffer = new byte[size]; //declare the size of the byte array with
	// size of the file
	// in.read(buffer); //read file
	// in.close(); //close file
	//
	// // Store text file data in the string variable
	// String str_data = new String(buffer);
	// System.out.println(str_data);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }

	// HTTP POST request
	public void postInsert(Post post) {

		String url = "http://loleconomy.altervista.org/src/service/PostService.php?action=insert";
		try {

			URL obj = new URL(url);
			HttpURLConnection con = (java.net.HttpURLConnection ) obj.openConnection();

			// add reuqest header
			con.setRequestMethod("POST");
			con.setRequestProperty("User-Agent", USER_AGENT);
			con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

			String urlParameters = "";

			urlParameters += "secret=l0renz0";
			urlParameters += "&id=" + post.getId();
			urlParameters += "&title=" + post.getTitle();
			urlParameters += "&image=" + post.getImg();
			urlParameters += "&source=" + post.getSource();

			// Send post request
			con.setDoOutput(true);
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();

			int responseCode = con.getResponseCode();
			System.out.println("\nSending 'POST' request to URL : " + url);
			System.out.println("Post parameters : " + urlParameters);
			System.out.println("Response Code : " + responseCode);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// print result
			System.out.println(response.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
